﻿package com.guavestudios.fcon.commands {
	import com.guavestudios.fcon.api.IControlableConsole;
	import com.guavestudios.fcon.api.INamedCommand;
	import com.guavestudios.fcon.plugins.ITextEditor;
	import com.guavestudios.fcon.plugins.script.Script;
	import com.guavestudios.fcon.plugins.SimpleScripter;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	
	/**
	* ...
	* @author Default
	*/
	public class ScriptCommand implements INamedCommand{
		private var se:ITextEditor;
		private var sc:SimpleScripter;
		private var editScript:Script;
		
		public function ScriptCommand() {
			
		}
		
		/* INTERFACE com.guavestudios.fcon.api.INamedCommand */
		
		public function execute(args:Array):void{
			var c:IControlableConsole = IControlableConsole(args.shift());
			if (args.length>0)
			{
				sc = SimpleScripter(c.getPlugin("SimpleScripter"));
				var scp:Script;
				switch (args[0])
				{
					case "edit":
						if (c.hasPlugin("SimpleEditor"))
						{
							se = ITextEditor(c.getPlugin("SimpleEditor"));
							if (!sc.hasScript(args[1]))
							{
								scp = new Script();
								scp.name = args[1];
								sc.addScript(scp);
							}
							else
							{
								scp = sc.getScript(args[1]);
							}
							
							se.setEdit(scp.getLines().join("\n"));
								
							editScript = scp;
							IEventDispatcher(se).addEventListener(Event.SELECT,editor_selected);
							se.show();
							
						}
						break;
					case "show":
						scp = sc.getScript(args[1]);
						if (scp != null)
						{
							var lns:Array = scp.getLines();
							
							for (var i:int = 0, len:int = lns.length; i<len; i++)
							{
								c.outputText(formatLine(i+1,lns[i]));
							}
						}
						else
						{
							c.outputText(getCommandName() + ": no script found");
						}
						break;
					case "add":
						scp = sc.getScript(args[1]);
						if (scp==null)
						{
							scp = new Script();
							scp.name = args[1];
							sc.addScript(scp);
						}
						scp.addLine(args.slice(2).join(" "));
						break;
					case "clear":
						scp = sc.getScript(args[1]);
						if (scp==null)
						{
							scp.setScript([]);
						}
						break;
					case "delete":
						sc.removeScript(args[1]);
						break;
					case "list":
						var a:Array = sc.getList();
						c.outputText(getCommandName() + ": List scripts("+a.length+"): ");
						for each (var s:Script in a)
						{
							c.outputText("\t"+s.name);
						}
						
						break;
					default:
						if (sc.hasScript(args[0]))
						{
							sc.executeScript(args[0]);
						}
						else
						{
							c.outputText(getCommandName()+": script not found: "+args[0]);
						}
						
						
				}
			}
			else
			{
				c.outputText("help: "+getCommandName()+" edit|show|add|clear|delete|list|scriptname");
			}
		}
		
		public function getCommandName():String{
			return "script";
		}
		public function formatHeader(scp:String):String
		{
			return scp+" content: "
		}
		public function formatLine(lineNumber:int,ln:Object):String
		{
			return lineNumber + ": " + ln;
		}
		
		private function editor_selected(e:Event):void {
			editScript.setScript(se.getEdit());
			sc = null;
			se = null;
		}
		
	}
	
}